package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Auther: tong
 * @Date: 2023/01/18/11:04
 * @Description:
 */
@Component
@Aspect
public class MyAspect {
    // 对哪些方法进行增强
    //@Pointcut("execution(* com.example.service.*.sayHello(..))")
    //public void pointCutName() {
    //}


    // 怎么增强
    //@Before("pointCutName()")
    //public void methodBefore() {
    //    System.out.println("方法被执行了");
    //}

    @Pointcut("@annotation(com.example.myannotation.InvokeLog)")
    public void pointCut1() {
    }

    @Before("pointCut1()")
    public void before(JoinPoint joinPoint) {
        System.out.println("before---");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 相当于执行目标方法
        //method.invoke();
        System.out.println(joinPoint);
    }

    @AfterReturning("pointCut1()")
    public void afterReturning(JoinPoint joinPoint) {
        System.out.println("afterReturning---");
        System.out.println(joinPoint);
    }

    @After("pointCut1()")
    public void after(JoinPoint joinPoint) {
        System.out.println("after---");
        System.out.println(joinPoint);
    }

    @AfterThrowing("pointCut1()")
    public void afterThrowing(JoinPoint joinPoint) {
        System.out.println("afterThrowing---");
        System.out.println(joinPoint);
    }

    //@Around("pointCut1()")
    //public void around(ProceedingJoinPoint point) {
    //    System.out.println("目标方法前---");
    //    try {
    //        // 目标方法执行
    //        point.proceed();
    //        System.out.println("目标方法后---");
    //    } catch (Throwable e) {
    //        e.printStackTrace();
    //        System.out.println("出现异常后---");
    //    }finally {
    //        System.out.println("finally中进行增强---");
    //    }
    //}
}
